package com.example.personalfoodadmin.activities;

import static com.example.personalfoodadmin.database.DatabaseHandler.ACTIVITYTYPE;
import static com.example.personalfoodadmin.database.DatabaseHandler.BMI;
import static com.example.personalfoodadmin.database.DatabaseHandler.BODYHEIGHT;
import static com.example.personalfoodadmin.database.DatabaseHandler.BODYWEIGHT;
import static com.example.personalfoodadmin.database.DatabaseHandler.CALORIES;
import static com.example.personalfoodadmin.database.DatabaseHandler.DATE;
import static com.example.personalfoodadmin.database.DatabaseHandler.DIETTYPE;
import static com.example.personalfoodadmin.database.DatabaseHandler.PRENAME;
import static com.example.personalfoodadmin.database.DatabaseHandler.SEX;
import static com.example.personalfoodadmin.database.DatabaseHandler.SURNAME;
import static com.example.personalfoodadmin.database.DatabaseHandler.USERPROFILE_ID;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.personalfoodadmin.R;
import com.example.personalfoodadmin.database.DatabaseHandler;

public class SelectProfile extends Activity
{
    private static boolean profileSelected = false;

    private ListView lv;

    public static final String PROFILE_EXTRA = "com.example.personalfoodadmin.PROFILE_EXTRA";
    
    public static final String COMPLETE_PROFILE = "com.example.personalfoodadmin.COMPLETE_EXTRA";

    public static final String PROFIlE_ID = "com.example.personfoodadmin.PROFILE_ID";

    private Cursor c;

    private ArrayList<String> profileList;

    private Hashtable<String, UserProfile> ht;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.show_profile);

        readFromDB(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.show_profile, menu);
        
        return true;
    }

    public void readFromDB(Context context)
    {
        DatabaseHandler t = new DatabaseHandler(this);

        SQLiteDatabase db = t.getWritableDatabase();

        this.c = db.query(DatabaseHandler.USERPROFILE_TABLE, new String[]
        { USERPROFILE_ID, PRENAME, SURNAME, SEX, DATE, BODYHEIGHT, BODYWEIGHT, BMI, ACTIVITYTYPE, DIETTYPE, CALORIES }, null, null, null, null, null);

        this.c.moveToFirst();
        
        this.lv = (ListView) this.findViewById(R.id.listview);
        
        this.profileList = new ArrayList<String>();
        
        this.ht = new Hashtable<String, UserProfile>();

        if (!this.c.isAfterLast())
        {
            do
            {
                UserProfile p = new UserProfile();
                
                p.setId(Long.parseLong(c.getString(0)));
                
                p.setPrename(c.getString(1));
                
                p.setSurname(c.getString(2));
                
                p.setSex(c.getString(3));
                
                p.setDate(c.getString(4));
                
                p.setBodyHeight(Float.parseFloat(c.getString(5)));
                
                p.setBodyWeight(Float.parseFloat(c.getString(6)));
                
                p.setBmi(Float.parseFloat(c.getString(7)));
                
                p.setActivityType(c.getString(8));
                
                p.setDietType(c.getString(9));
                
                p.setNeededCalories(Integer.parseInt(c.getString(10)));
                
                String s = p.getPrename()+" "+p.getSurname();
                
                this.ht.put(s, p);
                
                profileList.add(s);
            }
            while (this.c.moveToNext());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, profileList);
        
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                String selectedProfile = SelectProfile.this.lv.getItemAtPosition(position).toString();
                
                UserProfile p = SelectProfile.this.ht.get(selectedProfile);
                
                String completeProfile = "Sex: "+p.getSex()+"\nDate: "+p.getDate()+"\nBody Height: "+p.getBodyHeight()+"\nBody Weight: "+p.getBodyWeight()+"\nBMI: "+p.getBmi()+"\nActivity Type: "+p.getActivityType()+"\nDiet Type: "+p.getDietType()+"\nCalories Need: "+p.getNeededCalories();
                
                int _id = (int)p.getId();
                
                Intent i = new Intent(SelectProfile.this, MainMenue.class);
                
                i.putExtra(PROFILE_EXTRA, selectedProfile);
                
                i.putExtra(PROFIlE_ID, _id);
                
                i.putExtra(COMPLETE_PROFILE, completeProfile);
                
                SelectProfile.setSelected();
                
                SelectProfile.this.startActivity(i);
            }

        });

    }

    public static boolean profileSelected()
    {
        return SelectProfile.profileSelected;
    }

    public static void setSelected()
    {
        SelectProfile.profileSelected = true;
    }
    
    public void back(final View view)
    {
        Intent i = new Intent(this, MainMenue.class);
        this.startActivity(i);
    }

}
